package com.intel.factorytest.activity;

import java.io.IOException;

import com.intel.factorytest.R;
import com.intel.factorytest.log.LogWriter;

import android.util.Log;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import java.io.FileInputStream;

import android.os.SystemProperties;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.bluetooth.BluetoothAdapter;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;

import java.net.NetworkInterface;

import com.intel.factorytest.activity.BaseActivity;

public class BiosVersion extends BaseActivity {
    private static final String TAG = "BiosVersion";
    private static final String enter = "\n";
    private static final String space = "  ";
    TextView mBiosVersionView;
    TextView mHWVersion;
    TextView mBuildDate;
    Button mBackButton;
    String mBiosVersionText;
    String mSystemVersionText;
    final String FILE_NAME = "/sys/class/dmi/id/bios_version";
    private int[] statusStr = new int[]{R.string.un_test, R.string.test_pass};
    TextView mProoMark;

    TextView mSystemInfo_version;
    TextView mSystemInfo_modem;
    TextView mSystemInfo_serial;
    TextView systemInfo_imei1;
    TextView systemInfo_imei2;
    TextView systemInfo_bt_address;
    TextView systemInfo_wifi_mac;

    TextView systemInfo_front_camera_id;
    TextView systemInfo_rear_camera_id;
    TextView systemInfo_display_id;
    TextView systemInfo_memory_id;
    TextView systemInfo_flash_id;
    TextView systemInfo_ram_id;

    String CAMERA_ID_FRONT = "/sys/class/camera/id/front";
    String CAMERA_ID_FRONTID = "/sys/class/camera/id/frontid";
    String CAMERA_ID_REAR = "/sys/class/camera/id/rear";
    String CAMERA_ID_REARID = "/sys/class/camera/id/rearid";
    String mCameraIdFrontText;
    String mCameraIdFrontIdText;
    String mCameraIdRearText;
    String mCameraIdRearIdText;
    String front_camera_sensor_vendor;
    String rear_camera_sensor_vendor;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.bios_version);
        initView();
        mProoMark = (TextView) findViewById(R.id.proof_mark_textview);
        mBiosVersionView = (TextView) findViewById(R.id.bios_version_textview);
        mHWVersion = (TextView) findViewById(R.id.hw_version_textview);
        mBuildDate = (TextView) findViewById(R.id.buid_date_textview);
        String unKnowStr = getResources().getString(R.string.unknow_text);
        //mBiosVersionText = read(FILE_NAME);
        //mSystemVersionText = SystemProperties.get("ro.build.display.id", "unknown");
        mSystemVersionText = SystemProperties.get("ro.build.software.version", "unknown");
        mBiosVersionView.setText("SW Version: " + mSystemVersionText);//+"\nBIOS: "+mBiosVersionText //remove by wangsm
        String hwVerStr = SystemProperties.get("ro.boot.hwid", unKnowStr);
        mHWVersion.setText("HW Version: " + hwVerStr);
        String buidDate = SystemProperties.get("ro.build.date", unKnowStr);
        mBuildDate.setText(buidDate);
        String psn = getResources().getString(R.string.psn_str);
        String bt = getResources().getString(R.string.bt_str);
        String ft = getResources().getString(R.string.ft_str);
        String bbat = getResources().getString(R.string.bbat_str);
        String ant = getResources().getString(R.string.ant_str);
        String mmi = getResources().getString(R.string.mmi_str);
        String writeNum = getResources().getString(R.string.write_num_str);
        String dataCheck = getResources().getString(R.string.data_check_str);
        String[] str = new String[]{psn, bt, ft, bbat, ant, mmi, writeNum, dataCheck};
        int strLen = str.length;
        String prooMarkStr = SystemProperties.get("ril.meid", null);
        StringBuilder sb = new StringBuilder();
        if (prooMarkStr == null || prooMarkStr.equals("")) {
            sb.append(unKnowStr);
        } else {
            int len = prooMarkStr.length();

            if (len < strLen) {
                for (int j = 0; j < len; j++) {
                    char value = prooMarkStr.charAt(j);
                    Log.d(TAG, "the lenth<7 value is : " + value);
                    int index = value - '0';
                    if (index > 1 || index < 0) {
                        index = 0;
                    }
                    String status = getResources().getString(statusStr[index]);
                    sb = sb.append("<").append(j + 1).append(">").append(str[j]).append(space).append(status).append(enter);//value
                }
                for (int k = len; k < strLen; k++) {
                    Log.d(TAG, "the lenth<7 after value is : " + k);
                    sb = sb.append("<").append(k + 1).append(">").append(str[k]).append(space).append(getResources().getString(statusStr[0])).append(enter);//"0"
                }
            } else {
                for (int i = len - strLen, m = 0; i < len; i++, m++) {
                    char value = prooMarkStr.charAt(i);
                    int index = value - '0';
                    if (index > 1 || index < 0) {
                        index = 0;
                    }
                    String status = getResources().getString(statusStr[index]);
                    Log.d(TAG, "the lenth >= 7 value is : " + value);
                    sb = sb.append("<").append(m + 1).append(">").append(str[m]).append(space).append(status).append(enter);//value
                }
            }

        }

        //for test
        mCameraIdFrontText = read(CAMERA_ID_FRONT);
        mCameraIdFrontIdText = read(CAMERA_ID_FRONTID);
        mCameraIdRearText = read(CAMERA_ID_REAR);
        mCameraIdRearIdText = read(CAMERA_ID_REARID);
        Log.d(TAG, "mCameraIdFrontText = " + mCameraIdFrontText + ",mCameraIdFrontIdText = " + mCameraIdFrontIdText + ",mCameraIdRearText = " + mCameraIdRearText + ",mCameraIdRearIdText = " + mCameraIdRearIdText);
        checkSensorVendor(mCameraIdFrontIdText, 0);
        checkSensorVendor(mCameraIdRearIdText, 1);
        //for test end
        String markStr = sb.toString();
        mProoMark.setText(markStr);
        // ljr add
        mSystemInfo_version = (TextView) findViewById(R.id.systemInfo_version_textview);
        mSystemInfo_modem = (TextView) findViewById(R.id.systemInfo_modem_textview);
        mSystemInfo_serial = (TextView) findViewById(R.id.systemInfo_serial_textview);
        systemInfo_imei1 = (TextView) findViewById(R.id.systemInfo_imei1_textview);
        systemInfo_imei2 = (TextView) findViewById(R.id.systemInfo_imei2_textview);
        systemInfo_bt_address = (TextView) findViewById(R.id.systemInfo_bt_address_textview);
        systemInfo_wifi_mac = (TextView) findViewById(R.id.systemInfo_wifi_mac_textview);

        systemInfo_front_camera_id = (TextView) findViewById(R.id.systemInfo_front_camera_id_textview);
        systemInfo_rear_camera_id = (TextView) findViewById(R.id.systemInfo_rear_camera_id_textview);
        systemInfo_display_id = (TextView) findViewById(R.id.systemInfo_display_id_textview);
        systemInfo_memory_id = (TextView) findViewById(R.id.systemInfo_memory_id_textview);
        systemInfo_flash_id = (TextView) findViewById(R.id.systemInfo_flash_id_textview);
        systemInfo_ram_id = (TextView) findViewById(R.id.systemInfo_ram_id_textview);

        mSystemInfo_version.setText("Android Version: " + getSystemVersion());
        mSystemInfo_modem.setText("Modem: " + getModemName());
        mSystemInfo_serial.setText("Serial: " + getSerialNo());
        systemInfo_imei1.setText("IMEI1: " + getIMEI(0));
        systemInfo_imei2.setText("IMEI2: " + getIMEI(1));
        systemInfo_bt_address.setText("BT Address: " + getBluetoothAddress());
        systemInfo_wifi_mac.setText("WIFI MAC: " + getWifiMacAddress());

//        systemInfo_front_camera_id.setText("Front Camera_ID: " + front_camera_sensor_vendor + "_" + mCameraIdFrontText.replaceAll("\r|\n", ""));
//        systemInfo_rear_camera_id.setText("Rear Camera_ID: " + rear_camera_sensor_vendor + "_" + mCameraIdRearText.replaceAll("\r|\n", ""));
        systemInfo_display_id.setText("Display_ID: " + SystemProperties.get("ro.boot.lcdid", unKnowStr));
        systemInfo_memory_id.setText("Memory_ID(Flash/RAM): " + getMemeryID());
        systemInfo_flash_id.setText("FlashCID: " + SystemProperties.get("ro.boot.emmccid", unKnowStr));
        systemInfo_ram_id.setText("RAM ID: " + SystemProperties.get("ro.boot.ddrmanfid", unKnowStr) + "&" + SystemProperties.get("ro.boot.ddrmr8", unKnowStr));

        mItemTestFinish = true;
        /*        String markStr = psn + "1" + space + bt + "1" + space + ft + "1" + space + ant + "1" + space + mmi + "1" + space + writeNum + "1" + space + dataCheck + "1" + space;*/
        /*StringBuilder sb =  new StringBuilder();
        if(prooMarkStr != null && 0 < prooMarkStr.length() && prooMarkStr.length() < 8){

            for(int i=0;i<7;i++){
                sb = sb.append(str[i]).append(prooMarkStr.charAt(i)).append(space);
            }
        }else{
               sb.append(unKnowStr);
        }*/
/*        sb.append(psn).append("1").append(space).
           append(bt).append("1").append(space).
           append(ft).append("1").append(space).
           append(ant).append("1").append(space).
           append(mmi).append("1").append(space).
           append(writeNum).append("1").append(space).
           append(dataCheck).append("1").append(space);*/

        /*mBackButton = (Button) findViewById(R.id.button_reback);
        mBackButton.setVisibility(View.GONE);
        mBackButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                finish();

            }
        });*/
    }

    private String read(String fileName) {
        try {
            FileInputStream fis = new FileInputStream(fileName);
            byte[] buff = new byte[1024];
            int hasRead = 0;
            StringBuilder sb = new StringBuilder("");
            while ((hasRead = fis.read(buff)) > 0) {
                sb.append(new String(buff, 0, hasRead));
            }
            fis.close();
            return sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private void checkSensorVendor(String cameraic, int cameraid) {
        Log.d(TAG, "checkSensorVendor  cameraic = " + cameraic + ", cameraid = " + cameraid);
        int ic = 0;
        try {
            ic = (int) Double.parseDouble(cameraic == null ? "0" : cameraic);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        Log.d(TAG, "checkSensorVendor  ic = " + ic);
        switch (ic) {
            case 22133:
                if (cameraid == 0) {
                    front_camera_sensor_vendor = "OV5675";
                } else if (cameraid == 1) {
                    rear_camera_sensor_vendor = "OV5675";
                } else {
                    Log.d(TAG, "checkSensorVendor cameraid null ");
                }
                break;

            case 20533:
                if (cameraid == 0) {
                    front_camera_sensor_vendor = "GC5035";
                } else if (cameraid == 1) {
                    rear_camera_sensor_vendor = "GC5035";
                } else {
                    Log.d(TAG, "checkSensorVendor cameraid null ");
                }
                break;

            case 43:
                if (cameraid == 0) {
                    front_camera_sensor_vendor = "OV02B10";
                } else if (cameraid == 1) {
                    rear_camera_sensor_vendor = "OV02B10";
                } else {
                    Log.d(TAG, "checkSensorVendor cameraid null ");
                }
                break;

            case 736:
                if (cameraid == 0) {
                    front_camera_sensor_vendor = "GC02M1";
                } else if (cameraid == 1) {
                    rear_camera_sensor_vendor = "GC02M1";
                } else {
                    Log.d(TAG, "checkSensorVendor cameraid null ");
                }
                break;

            default:
                break;
        }
    }

    private String getMemeryID() {
        String ddrManfidGetString = null;
        String ddrManfidString = null;
        int ddrManfid = 0;
        String unKnowStr = getResources().getString(R.string.unknow_text);
        ddrManfidGetString = SystemProperties.get("ro.boot.ddrmanfid", unKnowStr);
        Log.d(TAG, "getMemeryID ddrManfidString = " + ddrManfidGetString);
        try {
            ddrManfid = (int) Double.parseDouble(ddrManfidGetString == null ? "0" : ddrManfidGetString);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        Log.d(TAG, "getMemeryID ddrManfid = " + ddrManfid);
        switch (ddrManfid) {
            case 1:
                ddrManfidString = "SAMSUNG";
                break;
            case 2:
                ddrManfidString = "QIMONDA";
                break;
            case 3:
                ddrManfidString = "ELPIDA";
                break;
            case 4:
                ddrManfidString = "ETRON";
                break;
            case 5:
                ddrManfidString = "NANYA";
                break;
            case 6:
                ddrManfidString = "HYNIX";
                break;
            case 7:
                ddrManfidString = "MOSEL";
                break;
            case 8:
                ddrManfidString = "WINBOND";
                break;
            case 9:
                ddrManfidString = "ESMT";
                break;
            case 10:
                ddrManfidString = "ESMT";
                break;
            case 11:
                ddrManfidString = "SPANSION";
                break;
            case 12:
                ddrManfidString = "SST";
                break;
            case 13:
                ddrManfidString = "ZMOS";
                break;
            case 14:
                ddrManfidString = "INTEL";
                break;
            case 254:
                ddrManfidString = "NUMONYX";
                break;
            case 255:
                ddrManfidString = "MICRON";
                break;

            default:
                ddrManfidString = "UNKNOW";
                break;
        }
        return "eMCP_" + ddrManfidString;
    }

    @Override
    public void finish() {
        super.finish();
    }

    protected void initView() {
        super.initView();
    }

    private String getSystemVersion() {
        return android.os.Build.VERSION.RELEASE;
    }

    private String getModemName() {
        return Build.getRadioVersion();
    }

    private String getSerialNo() {
        return Build.getSerial();
    }

    public String getIMEI(int slotIndex) {
        TelephonyManager telephonyManager = (TelephonyManager) this.getSystemService(TELEPHONY_SERVICE);
        return telephonyManager.getImei(slotIndex);
    }

    private String getBluetoothAddress() {
        BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();
        if (bluetooth != null) {
            return bluetooth.getAddress();
        }

        return null;
    }
    /*
    private String getWifiMacAddress() {
        WifiManager mWifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
        if (mWifiManager != null) {
            WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
            if (wifiInfo != null) {
                return wifiInfo.getMacAddress();
            }
        }

        return null;
    }
    */

    private String getWifiMacAddress() {
        WifiManager mWifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
        if (mWifiManager != null) {
            final String[] macAddresses = mWifiManager.getFactoryMacAddresses();
            if (macAddresses != null && macAddresses.length > 0) {
                Log.d(TAG, "getWifiMacAddress : " + macAddresses[0]);
                return macAddresses[0];
            }
        }

        return null;
    }
}